<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Recharge extends Auth_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('alipay_mdl');
    }

    /**
     * 用户充值首页
     */
    public function index(){
        if($_SESSION['user']['status'] != 3){
            show_error('您没有权限打开该页面');
        }
        $this->_view('index');
    }

    /**
     * 用户提交之后，生成支付宝参数，跳转链接
     */
    public function alipay(){
        if($_SESSION['user']['status'] != 3){
            show_error('您没有权限打开该页面');
        }

        $money = $this->input->post('money');
        if(!is_numeric($money)){
            show_error('参数错误！');
        }

        /**
         * 创建订单
         */
        $data = array(
            'user_id' => $_SESSION['user']['id'],
            'order_id' => date('YmdHis').$_SESSION['user']['id'].mt_rand(100,1000),
            'total_fee' => $money,
            'body' => "支付宝余额充值:$money",
            'create_time' => time(),
            'status' => '1'
        );
        $add_id = $this->alipay_mdl->create($data);

        if(!$add_id){
            show_error('服务器错误！');
        }

        //加载支付宝配置
        $this->config->load('alipay', TRUE);

        //加载支付宝支付请求类库
        require_once(APPPATH."third_party/alipay/alipay_submit.class.php");

        $submit = new AlipaySubmit($this->config->item('alipay'));

        $con = array(
            'service'           => 'create_direct_pay_by_user',
            'partner'           => $this->config->item('partner', 'alipay'),
            'payment_type'      => $this->config->item('payment_type', 'alipay'),
            'notify_url'        => $this->config->item('notify_url', 'alipay'),
            'return_url'        => $this->config->item('return_url', 'alipay'),
            'seller_email'      => $this->config->item('seller_email', 'alipay'),
            'out_trade_no'      => $data['order_id'], //订单号
            'subject'         => '在不在内部结算系统 余额充值', //订单名称
            'total_fee'         => $money, //付款金额
            'body'              => $data['body'],   //订单描述
            'show_url'          => site_url(),    //商品展示地址
            'anti_phishing_key' => '',
            'exter_invoke_ip'   => '',
            '_input_charset'    => $this->config->item('input_charset', 'alipay')
        );

        $body = $submit->buildRequestForm($con);

        $this->_view('pay',array('body'=>$body));
    }

    /**
     * 用户充值失败返回方法
     */
    public function notify(){
        show_error("充值失败！");
    }

    /**
     * 用户充值成功， 返回方法
     */
    public function areturn(){
        $get = $this->input->get();

        //Model
        $this->load->model('uinfo_mdl');
        $this->load->model('bill_mdl');

        //查询订单记录
        $order_id = $get['out_trade_no'];

        $this->db->where('order_id',$order_id);

        $res = $this->alipay_mdl->get_one();

        if(empty($res)){
            show_error('没有找到订单记录');
        }

        $this->db->trans_start();
        //添加充值记录
        $bill_data = array(
            'user_id' => $res['user_id'],
            'charges' => $res['total_fee'],
            'remarks' => $get['body'],
            'update_time' => $res['create_time']
        );
        $this->bill_mdl->create($bill_data);

        //更改余额
        $this->db->where('id',$res['user_id']);
        $this->db->set('money','money+'.$res['total_fee'],FALSE);
        $this->uinfo_mdl->update();

        //更改充值状态
        $this->db->where('id',$res['user_id']);
        $this->db->set('status',2);
        $this->db->set('buyer_email',$get['buyer_email']);
        $this->db->set('trade_no',$get['trade_no']);
        $this->alipay_mdl->update();

        //判断充值资金是否满足升级条件
        $this->load->model('users_mdl');
        $this->users_mdl->set_group($data['user_id']);

        $this->db->trans_complete();

        redirect(site_url('bill/show_bill'));
    }

}

/* End of file Recharge.php */
/* Location: ./application/controllers/Recharge.php */